<?php
	//get last image
	$dir = "./image/screen";
	$pattern = '\.(jpg|png|gif)$';
	$dc = opendir($dir);
	$screen_file = "";	
	while ($fn = readdir($dc)) {
		# Eliminate current directory, parent directory
		if (ereg('^\.{1,2}$',$fn)) continue;
		# Eliminate other pages not in pattern
		if (! ereg($pattern,$fn)) continue;	  
		
		$timedat = filemtime("$dir/$fn");
		if ($timedat > $newstamp) {
			$newstamp = $timedat;
			 $screen_file = "$dir/$fn";
		}
	}	
	
	
	
	if( file_exists($screen_file) ){
		$current_file = file_get_contents( "$dir/current.txt" );
		if( $current_file != $screen_file ){			
			file_put_contents( "$dir/current.txt", $screen_file );			
		}		
		echo $screen_file;
	}else{
		file_put_contents( "command.txt", "COMMAND\r\nCAPTURE");
		echo "wait";
	}
?>